home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Deutsche Edition 1
/
Deutsche Edition 1.iso
/
amok
/
071-080
/
amok78
/
sgconfiguration
/
sgconfigtest.mod
< prev
next >
Wrap
Text File
|
1993-11-04
|
11KB
|
362 lines
(*************************************************************************
:Program. SGConfigTest.mod
:Contents. test programm for SGConfiguraion
:Author. Hartmut Goebel [hG]
:Copyright. Public Domain
:Language. Oberon
:Translator. Amiga Oberon V2.25
:History. V1.0, 23 Apr 1992 [hG]
:Support. Olaf `Olsen' Barthel: fragment from `term' source
:Date. 07 May 1992 19:19:48
*************************************************************************)
MODULE SGConfigTest;
IMPORT
d := Dos,
e := Exec,
iff:= IFFParse,
ol := OberonLib,
req:= Requests,
s := SYSTEM,
sgc:= SGConfiguration,
str:= Strings;
CONST
NumConfigChunks = 4;
Version = 200; Revision = 42;
TYPE
ChunkStops = ARRAY 2 * NumConfigChunks OF LONGINT;
CONST
TEST = s.VAL(LONGINT,"TEST"); (* global FORM-Type *)
VERS = s.VAL(LONGINT,"VERS"); (* VersionInfo Follows *)
CNFG = s.VAL(LONGINT,"CNFG"); (* NewConfig follows *)
PORT = s.VAL(LONGINT,"PORT"); (* name of ARexx-Port *)
SCRN = s.VAL(LONGINT,"SCRN"); (* screen name *)
STRT = s.VAL(LONGINT,"STRT"); (* name of source file *)
StopAt = ChunkStops( (* That ar all the chunks we wanna read *)
TEST, CNFG,
TEST, PORT,
TEST, SCRN,
TEST, STRT);
TYPE
Configuration * = STRUCT
left*, top*, width*, height*: INTEGER;
screenDepth*: INTEGER;
aslLeft*, aslTop*, aslWidth*, aslHeight*: INTEGER;
END;
VersionInfoPtr = POINTER TO VersionInfo;
VersionInfo = STRUCT
ver, rev: INTEGER;
END;
CONST
ThisVersion = VersionInfo(Version,Revision);
StdConfig * = Configuration(
0,0,640,200, (* window edges *)
0, (* screendepth *)
20,20,100,200); (* asl edges *)
SGCTest = "SGCTest";
Template = "SETTING/K,USE/S,SAVE/S,WRITE/S,TRY/K,CD=CDFIRST/S";
numberOfArguments = 6;
argSetting = 0;
argUse = 1;
argSave = 2;
argWrite = 3;
argTry = 4;
argCDFirst = 5;
TYPE
argvType = ARRAY numberOfArguments OF (*e.ADDRESS*)LONGINT;
CONST
DefaultArguments = argvType(s.ADR(""),0,0,0,s.ADR(""),0);
VAR
Argv: argvType;
Arguments: d.RDArgsPtr;
DirLock: d.FileLockPtr;
DirBuffer, SettingsName : e.STRING;
string, string2: e.STRPTR;
PortName *: e.STRPTR;
ScreenName *: e.STRPTR;
StartUpName *: e.STRPTR; (* for ARexx-Scripts *)
Config *: Configuration;
PROCEDURE WriteConfig * (Name: ARRAY OF CHAR): BOOLEAN; (* $CopyArrays- *)
VAR
handle: iff.IFFHandlePtr;
success: BOOLEAN;
len: LONGINT;
BEGIN
success := FALSE;
handle := iff.AllocIFF();
IF handle # NIL THEN
handle.stream := s.VAL(s.ADDRESS,d.Open(Name,d.newFile));
IF handle.stream # NIL THEN
iff.InitIFFasDOS(handle);
IF iff.OpenIFF(handle,iff.write) = 0 THEN
(* Push outmost chunk onto stack *)
IF iff.PushChunk(handle,TEST,iff.idFORM,iff.IFFSizeUnknown) = 0 THEN
(* Add a version identifier *)
IF iff.PushChunk(handle,0,VERS,iff.IFFSizeUnknown) = 0 THEN
IF (iff.WriteChunkBytes(handle,ThisVersion,SIZE(ThisVersion)) = SIZE(ThisVersion))
& (iff.PopChunk(handle) = 0) THEN
success := TRUE;
END;
END;
(* Push the config chunk on the stack *)
IF success & (iff.PushChunk(handle,0,CNFG,iff.IFFSizeUnknown) = 0) THEN
IF (iff.WriteChunkBytes(handle,Config,SIZE(Config)) = SIZE(Config))
& (iff.PopChunk(handle) = 0) THEN
success := TRUE;
END;
END;
(* Now all the other chunks we wonna write *)
IF success & (PortName # NIL) THEN
IF (iff.PushChunk(handle,0,PORT,iff.IFFSizeUnknown) = 0) THEN
len := str.Length(PortName^);
IF (iff.WriteChunkBytes(handle,PortName^,len) # len)
OR (iff.PopChunk(handle) # 0) THEN
success := FALSE;
END;
END;
END;
IF success & (ScreenName # NIL) THEN
IF (iff.PushChunk(handle,0,SCRN,iff.IFFSizeUnknown) = 0) THEN
len := str.Length(ScreenName^);
IF (iff.WriteChunkBytes(handle,ScreenName^,len) # len)
OR (iff.PopChunk(handle) # 0) THEN
success := FALSE;
END;
END;
END;
IF success & (StartUpName # NIL) THEN
IF (iff.PushChunk(handle,0,STRT,iff.IFFSizeUnknown) = 0) THEN
len := str.Length(StartUpName^);
IF (iff.WriteChunkBytes(handle,StartUpName^,len) # len)
OR (iff.PopChunk(handle) # 0) THEN
success := FALSE;
END;
END;
END;
(* Seems that we're done, now try to pop the FORM chunk
* and return.
*)
IF iff.PopChunk(handle) # 0 THEN
success := FALSE;
END;
END;
iff.CloseIFF(handle); (* Close the handle (flush any pending data). *)
END;
(* Close the DOS handle itself. *)
IF ~d.Close(s.VAL(d.FileHandlePtr,handle.stream)) THEN
success := FALSE;
END;
END;
iff.FreeIFF(handle); (* And free the IFF handle. *)
END;
IF success & d.SetProtection(Name,LONGSET{d.execute}) THEN END;
RETURN success;
END WriteConfig;
PROCEDURE ReadConfig * (file: d.FileHandlePtr): BOOLEAN;
VAR
handle: iff.IFFHandlePtr;
success: BOOLEAN;
size, err: LONGINT;
prop: iff.StoredPropertyPtr;
chunk: iff.ContextNodePtr;
verInfo: VersionInfoPtr;
PROCEDURE ReadIFFString (VAR to: e.STRPTR);
VAR
string: e.STRING;
BEGIN
success := FALSE;
(* Read only as much char as in the chunk *)
IF chunk.size < SIZE(e.STRING) THEN
size := chunk.size;
ELSE
size := SIZE(e.STRING);
END;
(* The file read pointer is positioned just in front of the first data
* to be read, so let's don't disappoint iffparse and read it.
*)
IF iff.ReadChunkBytes(handle,string,size) = size THEN
ol.New(to,size+1);
IF to # NIL THEN
e.CopyMem(string,to^,size);
success := TRUE;
END;
END;
END ReadIFFString;
BEGIN
success := FALSE;
handle := iff.AllocIFF();
IF handle # NIL THEN
handle.stream := s.VAL(s.ADDRESS,file);
IF handle.stream # NIL THEN
iff.InitIFFasDOS(handle);
IF iff.OpenIFF(handle,iff.read) = 0 THEN
(* Collect version number ID if available *)
IF iff.PropChunk(handle,TEST,VERS) = 0 THEN
(* The following line tells iffparse to stop at the
* very beginning of one of the config chunks contained in a
* `TEST ' FORM chunk.
*)
IF iff.StopChunks(handle,StopAt,NumConfigChunks) = 0 THEN
(* Parse the file... *)
LOOP
err := iff.ParseIFF(handle,iff.iffParseScan);
CASE err OF
|iff.IFFErrEOF: EXIT; (* finished *)
|iff.IFFErrEOC: (* No action *)
|0:
chunk := iff.CurrentChunk(handle);
IF (chunk # NIL) & (chunk.type = TEST) THEN
CASE chunk.id OF
|CNFG:
prop := iff.FindProp(handle,TEST,VERS); (* Did we get a version ID? *)
IF prop # NIL THEN
(* Is it the file format we are able to read? *)
verInfo := s.VAL(s.ADDRESS,prop.data);
IF (verInfo.ver <= Version) & (verInfo.rev <= Revision) THEN
IF chunk.size < SIZE(Config) THEN
size := chunk.size;
ELSE
size := SIZE(Config);
END;
(* The file read pointer is positioned
* just in front of the first data
* to be read, so let's don't disappoint
* iffparse and read it.
*)
IF iff.ReadChunkBytes(handle,Config,size) = size THEN
success := TRUE; END;
END;
END;
|PORT: ReadIFFString(PortName);
|SCRN: ReadIFFString(ScreenName);
|STRT: ReadIFFString(StartUpName);
ELSE (* just ignore other Chunks *)
END; (* CASE chunk.id *)
END; (* IF chunk # NIL *)
ELSE
success := FALSE; EXIT; (* error uccured *)
END; (* CASE err *)
IF ~success THEN EXIT; END
END; (* LOOP *)
END; (* IF StopChunks *)
END; (* IF PropChunk *)
iff.CloseIFF(handle);
END; (* IF OpenIFF *)
(* ee MUST NOT close <file> *)
END; (* IF handle.stream *)
iff.FreeIFF(handle);
END; (* IF AllocHandle *)
RETURN success;
END ReadConfig;
BEGIN
req.Assert(d.dos.lib.version>=36,"SGConfigTest needs AmigaOS 2.0 or higher!");
sgc.BaseName := SGCTest;
Argv := DefaultArguments;
DirLock := NIL;
PortName := NIL; ScreenName := NIL; StartUpName := NIL;
Arguments := d.ReadArgs(Template,Argv,NIL);
IF Arguments = NIL THEN
IF d.PrintFault(d.IoErr(),"SBConfigTest: ") THEN END;
HALT(20);
END;
string := s.VAL(e.STRPTR,Argv[argSetting]);
string2 := s.VAL(e.STRPTR,Argv[argTry]);
SettingsName := string^;
IF sgc.ReadConfig(SettingsName,DirLock,
Argv[argCDFirst]=d.DOSTRUE,string2^,ReadConfig) THEN
IF d.NameFromLock(DirLock,DirBuffer,SIZE(DirBuffer)) THEN END;
d.PrintF("Configuration %s read from dir %s\n",
s.ADR(SettingsName), s.ADR(DirBuffer));
(*d.PrintF(" %s\n",PortName); d.PrintF(" %s\n",ScreenName); d.PrintF(" %s\n",StartUpName);*)
ELSE
IF d.PutStr("--> ReadError\n") = 0 THEN END;
SettingsName := string^; (* failed, use old text *)
PortName := s.ADR("ARexxPort");
ScreenName := s.ADR("MyScreen");
StartUpName := s.ADR("StartMeUp!!");
END;
IF Argv[argWrite] # d.DOSFALSE THEN
IF sgc.WriteConfig(SettingsName,DirLock,WriteConfig) THEN
IF d.NameFromLock(DirLock,DirBuffer,SIZE(DirBuffer)) THEN END;
d.PrintF("Configuration %s written to dir %s\n",s.ADR(SettingsName), s.ADR(DirBuffer));
ELSE
IF d.PutStr("Configuration write failed\n") = 0 THEN END;
END;
ELSIF Argv[argSave] # d.DOSFALSE THEN
IF sgc.UseConfig(TRUE,WriteConfig) THEN
IF d.PutStr("Configuration saved successfully\n") = 0 THEN END;
ELSE
IF d.PutStr("Configuration save failed\n") = 0 THEN END;
END;
ELSIF Argv[argUse] # d.DOSFALSE THEN
IF sgc.UseConfig(FALSE,WriteConfig) THEN
IF d.PutStr("Configuration used successfully\n") = 0 THEN END;
ELSE
IF d.PutStr("Configuration save failed\n") = 0 THEN END;
END;
END;
IF DirLock # NIL THEN d.UnLock(DirLock); END;
IF Arguments # NIL THEN d.FreeArgs(Arguments); END;
END SGConfigTest.